package com.zy.web.controller.log.collection;

import java.text.SimpleDateFormat;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zy.common.constant.Constants;
import com.zy.common.core.domain.entity.SysDictData;
import com.zy.common.core.redis.RedisCache;
import com.zy.common.exception.ServiceException;
import com.zy.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zy.common.annotation.Log;
import com.zy.common.core.controller.BaseController;
import com.zy.common.core.domain.AjaxResult;
import com.zy.common.enums.BusinessType;
import com.zy.log.collection.domain.LogCollection;
import com.zy.log.collection.service.ILogCollectionService;
import com.zy.common.utils.poi.ExcelUtil;
import com.zy.common.core.page.TableDataInfo;

/**
 * 日志采集Controller
 *
 * @author javachen
 * @date 2023-01-04
 */
@RestController
@RequestMapping("/log/logCollection")
public class LogCollectionController extends BaseController
{
    @Autowired
    private ILogCollectionService logCollectionService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询日志采集列表
     */
    @GetMapping("/list")
    public TableDataInfo list(LogCollection logCollection)
    {

        if(StringUtils.isEmpty(logCollection.getTableName())){
            throw new ServiceException("tableName不能为空");
        }

        List<SysDictData> logCollectionDictList = redisCache.getCacheObject(Constants.SYS_DICT_KEY+"log_collection");

        if(logCollectionDictList.stream()
                .filter(item->item.getDictValue()
                .equals(logCollection.getTableName()))
                .findAny()
                .isPresent()==false){
            throw new ServiceException("tableName不存在");
        }

        if(null!=logCollection.getCollectionTime()){

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String collectionTimeStr = format.format(logCollection.getCollectionTime());

            logCollection.setCollectionTimeStart(collectionTimeStr+" 00:00:00");
            logCollection.setCollectionTimeEnd(collectionTimeStr+" 23:59:59");
        }

        startPage();
        List<LogCollection> list = logCollectionService.selectLogCollectionList(logCollection);
        return getDataTable(list);
    }

    /**
     * 查询日志详情
     */
    @PostMapping(value = "/getDetail")
    public AjaxResult getDetail(LogCollection logCollection)
    {

        if(StringUtils.isEmpty(logCollection.getTableName())){
            throw new ServiceException("tableName不能为空");
        }

        List<SysDictData> logCollectionDictList = redisCache.getCacheObject(Constants.SYS_DICT_KEY+"log_collection");

        if(logCollectionDictList.stream()
                .filter(item->item.getDictValue()
                        .equals(logCollection.getTableName()))
                .findAny()
                .isPresent()==false){
            throw new ServiceException("tableName不存在");
        }

        return AjaxResult.success(logCollectionService.getDetail(logCollection));
    }

    /**
     * 新增日志采集
     */
    @PostMapping
    public AjaxResult add(@RequestBody LogCollection logCollection)
    {
        return toAjax(logCollectionService.insertLogCollection(logCollection));
    }

}
